﻿''' <summary>
''' PreDefined Modifiers
''' </summary>
''' <remarks>
''' Predifined Modifiers dont use MS Anti-XSS Library because of licence problems.
''' </remarks>
Public Class Modifiers

    ''' <summary>
    ''' Escapes scripting characters according HTML Specicifications. 
    ''' </summary>
    ''' <remarks>Internally uses <see cref="HttpUtility.HtmlEncode">HttpUtility.HtmlEncode</see></remarks>
    Public Class HTML
        Implements IModifier(Of String)

        'UTF8 Characters
        Private Const HtmlStart As String = "&#"
        Private Const HtmlEnd As String = ";"

        ''' <summary>
        ''' Converts the specified input.
        ''' </summary>
        ''' <param name="input">The input.</param>
        ''' <returns>HtmlEncoded input</returns>
        Public Function Convert(ByVal input As String) As String Implements IModifier(Of String).Convert
            Return HTML.ConvertStatic(input)
        End Function

        ''' <summary>
        ''' Converts the specified input.
        ''' </summary>
        ''' <param name="Input">The input.</param>
        ''' <returns></returns>
        Public Shared Function ConvertStatic(ByVal Input As String) As String
            If Input Is Nothing Then Return Nothing

            Dim NewOutput As New Text.StringBuilder(Input.Length * 2)
            For Each currentChar As Char In Input

                Dim CharVal As Integer = AscW(currentChar)
                If HtmlSafeChar(CharVal) Then
                    NewOutput.Append(currentChar)

                Else 'Encode
                    NewOutput.Append(HtmlStart).Append(CharVal).Append(HtmlEnd)

                End If

            Next currentChar


            Return NewOutput.ToString()
        End Function



        'Ranges
        Private Const AlphaA As Integer = Asc("A")
        Private Const AlphaZ As Integer = Asc("Z")
        Private Const AlphaLowerA As Integer = Asc("a")
        Private Const AlphaLowerZ As Integer = Asc("z")

        Private Const NumericalZero As Integer = Asc("0")
        Private Const NumericalNine As Integer = Asc("9")
        Private Const Space As Integer = Asc(" ")
        Private Const Comma As Integer = Asc(",")


        ''' <summary>
        ''' Checks HTMLs the safe char.
        ''' </summary>
        ''' <param name="character">The character.</param>
        ''' <returns></returns>
        Private Shared Function HtmlSafeChar(ByVal character As Integer) As Boolean

            Dim Valid As Boolean = _
            (character >= AlphaA AndAlso character <= AlphaZ) OrElse _
            (character >= AlphaLowerA AndAlso character <= AlphaLowerZ) OrElse _
            (character >= NumericalZero AndAlso character <= NumericalNine) OrElse _
            (character = Space) OrElse _
            (character = Comma)

            Return Valid
        End Function
    End Class

    ''' <summary>
    ''' Encodes characters eccording to RFC (internally uses <see cref="Web.HttpUtility.UrlEncode">HttpUtility.URLEncode()</see>)
    ''' </summary>
    Public Class URL
        Implements IModifier(Of String)

        Public Function Convert(ByVal input As String) As String Implements IModifier(Of String).Convert
            Return Web.HttpUtility.UrlEncode(input)
        End Function
    End Class

    ''' <summary>
    ''' Encodes characters for XML output (as value)
    ''' </summary>
    ''' <remarks>
    ''' Internall uses <see cref="Modifiers.HTML">Modifiers.HTML</see>
    ''' </remarks>
    Public Class XML
        Implements IModifier(Of String)

        Public Function Convert(ByVal input As String) As String Implements IModifier(Of String).Convert
            Dim HtmlEncoder As New HTML()
            Return HtmlEncoder.Convert(input)
        End Function
    End Class

End Class